﻿using System;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace IFD2
{
    public partial class DepFltSeatMap : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["ACL"].ToString().IndexOf("DA1") < 0)
                {
                    Response.Redirect("unauthorized.aspx");
                }
                lblOpAirline.Text = Session["AIRLINE"].ToString();
                if (Request.QueryString["flt_no"] != null)
                {
                    //if (Session["AIRLINE"].ToString() != Request.QueryString["airline"])
                    //{
                    //    Response.Redirect("unauthorized.aspx");
                    //}
                    txtFltNo.Text = Request.QueryString["flt_no"];
                    txtFltDate.Text = Request.QueryString["flt_date"];
                    lblOpAirline.Text = Request.QueryString["airline"];
                }
                else
                {
                    if (Session["FLT_NO"] != null)
                        txtFltNo.Text = Session["FLT_NO"].ToString();
                    if (Session["FLT_DATE"] != null)
                        txtFltDate.Text = Convert.ToDateTime(Session["FLT_DATE"].ToString()).ToShortDateString();
                }
                hidFltNo.Value = txtFltNo.Text;
                hidFltDate.Value = txtFltDate.Text;

                lbtnQuery_Click(sender, e);

            }
            btnDelete.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");
            btnAdd.Attributes.Add("onclick", "javascript:return confirm('Are you sure?');");

            lblError.Text = "";
            if (lblOpAirline.Text != Session["AIRLINE"].ToString())
                btnAdd.Enabled = false;
            else
                btnAdd.Enabled = true;
        }

        protected void lbtnQuery_Click(object sender, EventArgs e)
        {
            try
            {
                DateTime.Parse(txtFltDate.Text);
            }
            catch
            {
                lblError.Text = "Invalid date format!";
                return;
            }

            Session["FLT_NO"] = txtFltNo.Text;
            Session["FLT_DATE"] = txtFltDate.Text;
            hidFltNo.Value = txtFltNo.Text;
            hidFltDate.Value = txtFltDate.Text;
            ctlSeatMatching.compare(lblOpAirline.Text, txtFltNo.Text, Convert.ToDateTime(txtFltDate.Text));

            int ret_code;
            ret_code = ctl_seat_map.RetreiveSeatMap('F', lblOpAirline.Text, txtFltNo.Text, "", Convert.ToDateTime(txtFltDate.Text));
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("DA2") < 0)
            {
                lblError.Text = "Unauthorized!";
                return;
            }
            string[] strArray;
            string key;
            if (Request.Form["CELL"] == null)
            {
                lblError.Text = "Please select a record.";
                return;
            }
            key = Request.Form["CELL"];
            strArray = key.Split('|');

            int ret_code;

            SqlCommand cmd = new SqlCommand();
            lblError.Text = "";
            try
            {
                cmd.CommandText = "bll_flt_delete_seat_cell";
                SqlParameter para = cmd.Parameters.AddWithValue("@airline", lblOpAirline.Text);
                para = cmd.Parameters.AddWithValue("@flt_no", hidFltNo.Value);
                para = cmd.Parameters.AddWithValue("@flt_date", hidFltDate.Value);
                para = cmd.Parameters.AddWithValue("@x", strArray[0]);
                para = cmd.Parameters.AddWithValue("@y", strArray[1]);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());
                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblError.Text = "Done.";
                    lbtnQuery_Click(sender, e);
                    return;
                }
                else if (ret_code == 1)
                    lblError.Text = "Deleting failed!";
                else
                    lblError.Text = "Error " + ret_code.ToString() + "! Contact system adminstartor!";
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = ex.Message;
                else
                    lblError.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("DA2") < 0)
            {
                lblErrAdd.Text = "Unauthorized!";
                return;
            }
            txtSeatType.Text = txtSeatType.Text.Trim();
            if (txtSeatType.Text.Length == 0)
            {
                lblErrAdd.Text = "Please input reserve type!";
                return;
            }
            if (lblOpAirline.Text != Session["AIRLINE"].ToString())
            {
                lblErrAdd.Text = "Can not change other airline flight seat!";
                return;
            }
            int ret_code;

            SqlCommand cmd = new SqlCommand();
            lblErrAdd.Text = "";
            try
            {
                cmd.CommandText = "bll_flt_add_seat_cell";
                SqlParameter para = cmd.Parameters.AddWithValue("@airline", lblOpAirline.Text);
                para = cmd.Parameters.AddWithValue("@flt_no", hidFltNo.Value);
                para = cmd.Parameters.AddWithValue("@flt_date", hidFltDate.Value);
                para = cmd.Parameters.AddWithValue("@x", txtX.Text);
                para = cmd.Parameters.AddWithValue("@y", txtY.Text);
                para = cmd.Parameters.AddWithValue("@cabin", lstAcCabins.SelectedValue);
                para = cmd.Parameters.AddWithValue("@cell_type", lstSpaceTypes.SelectedValue);
                para = cmd.Parameters.AddWithValue("@row", txtRow.Text);
                para = cmd.Parameters.AddWithValue("@col", lstCols.SelectedValue);
                para = cmd.Parameters.AddWithValue("@load_area", lstLoadArea.SelectedValue);
                para = cmd.Parameters.AddWithValue("@seat_type", txtSeatType.Text.ToUpper());
                para = cmd.Parameters.AddWithValue("@class", lstClasses.SelectedValue);
                para = cmd.Parameters.AddWithValue("@attributes", txtAttributes.Text.ToUpper());
                para = cmd.Parameters.AddWithValue("@ori_airport", lstOriAirports.SelectedValue);
                para = cmd.Parameters.AddWithValue("@des_airport", lstDesAirports.SelectedValue);
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());

                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblErrAdd.Text = "Done.";
                    lbtnQuery_Click(sender, e);
                    return;
                }
                else if (ret_code == 1)
                    lblErrAdd.Text = "Inserting failed!";
                else if (ret_code == 2)
                    lblErrAdd.Text = "Invalid Row!";
                else if (ret_code == 3)
                    lblErrAdd.Text = "Invalid X or Y!";
                else if (ret_code == 4)
                    lblErrAdd.Text = "Seat number existed!";
                else if (ret_code == 5)
                    lblErrAdd.Text = "Y exceeded 15!";
                else
                    lblErrAdd.Text = "Error:" + ret_code.ToString();
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblErrAdd.Text = ex.Message;
                else
                    lblErrAdd.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }


        protected void btnLog_Click(object sender, EventArgs e)
        {
            Response.Redirect("DepFltSeatLog.aspx?flt_no=" + hidFltNo.Value + "&flt_date=" + hidFltDate.Value);
        }

        protected void btnChange_Click(object sender, EventArgs e)
        {
            if (Session["ACL"].ToString().IndexOf("DA2") < 0)
            {
                lblErrChange.Text = "Unauthorized!";
                return;
            }
            if (lstEditTarget.SelectedValue == "S" || lstEditTarget.SelectedValue == "C")
            {
                if (txtNewValue.Text.Trim().Length > 2)
                {
                    lblErrChange.Text = "New value is too long!";
                    return;
                }
            }

            int ret_code;
            SqlCommand cmd = new SqlCommand();
            lblErrChange.Text = "";
            try
            {
                cmd.CommandText = "bll_flt_batch_set_seats_reserving";
                SqlParameter para = cmd.Parameters.AddWithValue("@airline", lblOpAirline.Text);
                para = cmd.Parameters.AddWithValue("@flt_no", hidFltNo.Value);
                para = cmd.Parameters.AddWithValue("@flt_date", hidFltDate.Value);
                para = cmd.Parameters.AddWithValue("@change_target", lstEditTarget.SelectedValue);
                para = cmd.Parameters.AddWithValue("@new_value", txtNewValue.Text.Trim());
                para = cmd.Parameters.AddWithValue("@where_clause", txtWhere.Text);
                //para = cmd.Parameters.AddWithValue("@user_airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());

                ret_code = TinyDAL.Exec(ref cmd);
                if (ret_code == 0)
                {
                    lblErrChange.Text = "Done.";
                    lbtnQuery_Click(sender, e);
                    ctlSeatMatching.compare(lblOpAirline.Text, txtFltNo.Text, Convert.ToDateTime(txtFltDate.Text));
                    return;
                }
                else if (ret_code == 1)
                    lblErrChange.Text = "Illegal verbs";
                else if (ret_code == 2)
                    lblErrChange.Text = "The aircraft is not belong to the airline!";
                else if (ret_code == 3)
                    lblErrChange.Text = "Invalid target!";
                else if (ret_code == 4)
                    lblErrChange.Text = "Updating failed!";
                else
                    lblErrChange.Text = "Error:" + ret_code.ToString();
            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblErrChange.Text = ex.Message;
                else
                    lblErrChange.Text = "Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
            }
        }
        protected void lstAcCabins_SelectedIndexChanged(object sender, EventArgs e)
        {
            lstClasses.DataBind();
        }
        protected void lstAirports_SelectedIndexChanged(object sender, EventArgs e)
        {
            lstClasses.DataBind();
        }
        protected void lstClasses_DataBound(object sender, EventArgs e)
        {
            lstClasses.Items.Add(new ListItem("* - (No Control) ", "*"));
        }


        protected void lstOriAirports_DataBound(object sender, EventArgs e)
        {
            lstOriAirports.Items.Add(new ListItem("* - (No Control) ", "*"));
            //if(!IsPostBack)
                lstOriAirports.SelectedValue = "*";
        }
        protected void lstDesAirports_DataBound(object sender, EventArgs e)
        {
            lstDesAirports.Items.Add(new ListItem("* - (No Control) ", "*"));
            //if (!IsPostBack)
                lstDesAirports.SelectedValue = "*";
        }
    }
}